define(window.backboneCommon.getViewDefine("finance.agentTransfer"), function ($,require,Collection,tpl,ViewTransferIn,ViewTransferOut) {
    var defaultsOptions={
        getSurplusAmountCallback:null,
        transferCallback:null
    };
    var pagePrivate = {
        getDefaults :function () {
            return defaultsOptions
        },
        getOptions : function (options) {
            options=$.ExtCommon.isCheckEmpty(options)?{}:options;
            return $.ExtCommon.initMergeJson(this.getDefaults(),options);
        }
    };
    var backboneView= Backbone.View.extend({
        events: {
            "click #tb_listAgentTransfer .transferIn": "showTransferIn",
            "click #tb_listAgentTransfer .transferOut": "showTransferOut"
        },
        initialize: function () {
            this.listenTo(this.model, 'bind:AgentListTransfer', this.bindList);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(options){
            this.elementModel={
                table:$("#tb_listAgentTransfer")
            };
            this.extObject={};
            this.pageData={initPage:false};
            this.pageOptions=$.ExtCommon.initMergeJson(pagePrivate.getOptions(),options);
            this._initInputSearch();
        },
        initPage:function(){
            if(!this.pageData.initPage){
                this.pageData.initPage=true;
                this.initViewAttach();
                this._initPageList();
            }
        },
        initViewAttach:function () {
            var self=this;
            this.viewAttach={};
            this.viewAttach.transferIn=new ViewTransferIn({pElement:"#container_transferIn",
                submitCallback: function () {self.pageOptions.transferCallback()}
            });
            this.viewAttach.transferOut=new ViewTransferOut({pElement:"#container_transferOut",
                submitCallback: function () {self.pageOptions.transferCallback()}
            });
        },
        _initInputSearch:function () {
            var self=this;
            self.extObject.inputSearch=$("#panel_inputSearch_agentTransfer").ExtInputSearch({
                placeholder:"广告主ID,广告主名称",
                searchCallback:function () {
                    self.refreshList();
                }
            })
        },
        _initPageList: function () {
            var self=this;
            self.extObject.table=self.elementModel.table.ExtDataTable({
                columns: [
                    {data:"id", class: "w-60"},
                    {data:"customerName", class: ""},
                    {data:"serviceCost",class: "w-300"},
                    {data:null,class: "w-300 text-center"}
                ],
                infoExt:{
                    ajaxDataPara:function(){
                        var param= {};
                        var keyword = self.extObject.inputSearch.getValue();
                        if (!$.ExtCommon.isCheckEmpty(keyword)) {param.search = keyword;}
                        return param;
                    },
                    ajaxWholeCallback:function(param){self.collection.getAgentListTransfer(param)}
                },
                createdRow: function (row, data) {
                    var elementTd=$("td", row);
                    elementTd.eq(2).html(data.serviceCost+"%");
                    elementTd.eq(3).html('<a href="javascript:" class="btn-operate longterm transferIn">转入资金</a><a href="javascript:" class="btn-operate longterm transferOut">转出资金</a>');
                },
                drawCallback: function () {
                    $.ExtNumeric.initAll();
                }
            });
        },
        bindList:function(){
            var data=this.model.get("dataAgentListTransfer");
            this.extObject.table.ajaxWholeRefreshData(data);
        },
        refreshList: function () {this.extObject.table.refresh()},
        
        showTransferIn:function (e) {
            var tr = $(e.currentTarget).parents('tr:first');
            var rowData = this.extObject.table.getRowInfo(tr).data();
            var newJson=$.ExtCommon.getJsonClone(rowData);
            newJson.agentSurplusAmount=this.pageOptions.getSurplusAmountCallback();
            this.viewAttach.transferIn.init(newJson);
        },
        showTransferOut:function (e) {
            var tr = $(e.currentTarget).parents('tr:first');
            var rowData = this.extObject.table.getRowInfo(tr).data();
            var newJson=$.ExtCommon.getJsonClone(rowData);
            this.viewAttach.transferOut.init(newJson);
        }
    });
    var viewModel=function(options){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el: options.pElement,collection:this.collection,model:this.model});
        this.view.initMain(options);
    };
    viewModel.prototype={
        init:function () {
            this.view.initPage();
        }
    };
    return viewModel;
});